package sporteam.web.client;



import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FriendActivityUtil {

	
	
	public static ArrayList<GeoPtr> senderTrack;
	public static ArrayList<GeoPtr> recieverTrack;
	

	
	
	public static ClickHandler friendActivityFlexTableClickHandler(final FlexTable table) {
		
		return new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				HTMLTable.Cell cell = table.getCellForEvent(event);
				if (cell == null) {
					//Window.alert("cell is null");
				} 
				else if(cell.getRowIndex() == 0){
					// DO NOTHING !!!
				}
				else {
					
					String track_name = table.getText(cell.getRowIndex(), 0);
					for(Activity activity : Sporteam_web.activities){
						String my_track_name = activity.isImTheSender(Sporteam_web.miniUser.getFacebook_id()) ? activity.getSenderTrack().getName() :activity.getReceiverTrack().getName();
						Track senderTrack = activity.getSenderTrack();
						Track receiverTrack = activity.getReceiverTrack();
						if(track_name.equals(my_track_name)){
							//Sporteam_web.showMaps();
							if(senderTrack.GetGeoPtrList().size() > 0 && receiverTrack.GetGeoPtrList().size() > 0){
								Sporteam_web.jso_map = Sporteam_web.initializeMap("map", senderTrack.GetGeoPtrList().get(0));
								Sporteam_web.jso_map2 = Sporteam_web.initializeMap("map2", receiverTrack.GetGeoPtrList().get(0));
							showActivity(activity);
							break;
							}
						}

					}
				}

			}
		};
		
		



		
	}




	public static void showActivity(Activity activity) {
		HorizontalPanel activityDisplayPanel = new HorizontalPanel();
		activityDisplayPanel.setStyleName("activityDisplayPanel");
		
		/*build two tables describing both tracks in activity*/
		//Sender table
		FlexTable senderActivityInfoTable = new FlexTable();
		buildActivityInfoTableHeader(senderActivityInfoTable, "Sender");
		senderActivityInfoTable.setWidget(1, 0, createUserBox(activity.getSenderName(), activity.getSenderID()));
		fillActivityInfoTable(senderActivityInfoTable, activity.getSenderTrack());
			//Add style to sender-table
			senderActivityInfoTable.setStyleName("senderTable");
		
		//Receiver table
		FlexTable receiverActivityInfoTable = new FlexTable();
		buildActivityInfoTableHeader(receiverActivityInfoTable, "Receiver");
		receiverActivityInfoTable.setWidget(1, 0, createUserBox(activity.getReceiverName(), activity.getReceiverID()));
		fillActivityInfoTable(receiverActivityInfoTable, activity.getReceiverTrack());
			//Add style to sender-table
			receiverActivityInfoTable.setStyleName("receiverTable");
			
		// Add style common to both tables:
		receiverActivityInfoTable.addStyleName("activityTable");
		senderActivityInfoTable.addStyleName("activityTable");
		receiverActivityInfoTable.getRowFormatter().addStyleName(0,
				"activityTableHeader");
		senderActivityInfoTable.getRowFormatter().addStyleName(0,
				"activityTableHeader");
			
		//attach tables to activityDisplayPanel 
		activityDisplayPanel.add(senderActivityInfoTable);
		activityDisplayPanel.add(receiverActivityInfoTable);
		
		//Remove current showing panel and display acitivtyDisplayPanel
		Sporteam_web.removeCurrentWidget();
		Sporteam_web.showMaps();
		Sporteam_web.displayWidget(activityDisplayPanel);
		
		//draw tracks on maps
		Sporteam_web.showTracksOnMaps(activity.getSenderTrack().GetGeoPtrList(), activity.getReceiverTrack().GetGeoPtrList());
	}




	private static void buildActivityInfoTableHeader(FlexTable table, String part) {
		table.setText(0, 0, part);
		table.setText(0, 1, "Date");
		table.setText(0, 2, "Distance");
		table.setText(0, 3, "Time");
		table.setText(0, 4, "Calories");
	}
	
	//Returns a Vertical panel containing name and picture associated with id
	public static VerticalPanel createUserBox(String name, String id){
		VerticalPanel userBox = new VerticalPanel();
		
		// Construct  name label
		Label userNameLabel = new Label();
		userNameLabel.setText(name);
		
		// Construct image
		Image userImage = new Image();
		String userImageURL = "https://graph.facebook.com/"
				+ id + "/picture?type=square";
		userImage.setUrl(userImageURL);
		
		// Assemble userBox
		userBox.add(userNameLabel);
		userBox.add(userImage);
		
		return userBox;
	}
	
	public static void fillActivityInfoTable(FlexTable table, Track track){
		table.setText(1, 1, track.getName());
		table.setText(1, 2, Sporteam_web.translateDistance(track.GetDistance()));
		table.setText(1, 3, Sporteam_web.translateTrackTime(track.GetTrackTime()));
		table.setText(1, 4, Double.toString(Math.floor(track.getCalories())));
	}
	
	

	
	
	
	
	
	
}
